﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmKhoMaVach : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private FormStatus formStatus = FormStatus.View;
        private bool allowCloseForm = true;
        private TonKhoBOCollection TonKhoBOCollection = null;
        private MaVachBOCollection MaVachBOCollection = null;

        private string COLUMN_FIRST_TonKho = "Ten";
        private string COLUMN_SECOND_TonKho = "SoThuTu";

        private string COLUMN_FIRST_MaVach = "Ten";
        private string COLUMN_SECOND_MaVach = "HeSo";
        #endregion variant

        //=====================================
        #region form
        public FrmKhoMaVach()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmKhoMaVach_Load);
            this.Shown += new EventHandler(FrmKhoMaVach_Shown);
            this.FormClosing += new FormClosingEventHandler(FrmKhoMaVach_FormClosing);

            this.btnRefresh.Click += new EventHandler(btnRefresh_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);

            this.grdViewTonKho.RowCellStyle += new RowCellStyleEventHandler(grdView_RowCellStyle);
            this.grdViewMaVach.RowCellStyle += new RowCellStyleEventHandler(grdView_RowCellStyle);

            this.grdViewTonKho.ShowingEditor += new CancelEventHandler(grdView_ShowingEditor);
            this.grdViewMaVach.ShowingEditor += new CancelEventHandler(grdView_ShowingEditor);
        }

        private void FrmKhoMaVach_Shown(object sender, EventArgs e)
        {
            this.grdViewTonKho.Focus();
        }

        private void FrmKhoMaVach_Load(object sender, EventArgs e)
        {
            RefreshData();

            Utility.InitControl(this.reLookUpMatHangID);
            Utility.InitControl(this.reLookUpDonViTinh);
            Utility.InitControl(this.reLookUpDanhMucKhoID);
            this.grdViewTonKho.FocusedRowChanged += new FocusedRowChangedEventHandler(grdViewTonKho_FocusedRowChanged);
        }

        private void FrmKhoMaVach_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
        }
        #endregion form

        //=====================================
        #region control
        #endregion control

        //=====================================
        #region button
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid
        private void grdViewTonKho_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            FilterTonKho();
        }

        private void grdView_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView grdView = (GridView)sender;

            bool del = StaticUtil.GetBool(grdView.GetFocusedRowCellValue("DELE"));
            string fieldName = grdView.FocusedColumn.FieldName;

            if (del && fieldName != "DELE")
                e.Cancel = true;
            else
                if (fieldName == "DELE" && grdView.FocusedRowHandle == GridControl.NewItemRowHandle)
                    e.Cancel = true;
        }

        private void grdView_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            bool del = StaticUtil.GetBool(this.grdViewTonKho.GetRowCellValue(e.RowHandle, "DELE"));

            if (del)
            {
                e.Appearance.Font = MyStyle.DELETE_FONT;
                e.Appearance.ForeColor = MyStyle.DELETE_COLOR;
            }
        }
        #endregion grid

        //=====================================
        #region function
        private void FilterTonKho()
        {
            Session.TonKhoID = StaticUtil.GetRealInt(this.grdViewTonKho.GetFocusedRowCellValue("TonKhoID"));

            if (this.formStatus == FormStatus.Edit)
                this.grdViewMaVach.OptionsView.NewItemRowPosition = (Session.TonKhoID != null ? NewItemRowPosition.Bottom : NewItemRowPosition.None);

            if (Session.TonKhoID == null)
                this.grdViewMaVach.ActiveFilterString = "TonKhoID=" + int.MaxValue;
            else
                this.grdViewMaVach.ActiveFilterString = "TonKhoID=" + Session.TonKhoID.Value.ToString();

            this.grdViewMaVach.CheckLoaded();
            //SetSttMaVach();
        }

        private void RefreshData()
        {

            #region

            DanhMucBOList donViTinhBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DonViTinh);
            Utility.SetData(this.reLookUpDonViTinh, donViTinhBOList, donViTinhBOList.Count);

            DanhMucBOList danhMucKhoBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DanhMucKho);
            Utility.SetData(this.reLookUpDanhMucKhoID, danhMucKhoBOList, danhMucKhoBOList.Count);

            MatHangBOList matHangBOList = MatHangBOList.GetMatHangBOList();
            Utility.SetData(this.reLookUpMatHangID, matHangBOList, matHangBOList.Count);

            #endregion BOList

            this.TonKhoBOCollection = TonKhoBOCollection.GetTonKhoBOCollection();
            this.MaVachBOCollection = MaVachBOCollection.GetMaVachBOCollection();

            this.bdSourceTonKho.DataSource = this.TonKhoBOCollection.Clone();
            this.bdSourceMaVach.DataSource = this.MaVachBOCollection.Clone();

            FilterTonKho();
        }

        #endregion function

        private void toolStripMenuItemThanhToan_Click(object sender, EventArgs e)
        {
            int _MaVachID = StaticUtil.GetInt(this.grdViewMaVach.GetFocusedRowCellValue("MaVachID"), -1);

            if (_MaVachID != -1)
            {
                FrmDieuChuyenKho.daluu = 0;
                FrmDieuChuyenKho.MaVachBO = Model.MaVachBO.GetRootMaVachBO(_MaVachID);
                FrmDieuChuyenKho frm = new FrmDieuChuyenKho();
                frm.ShowDialog();
            }
            else
            {
                MessageBox.Show("Chưa chọn Hàng hóa và Mã vạch để điều chuyển! ", "Thông báo", MessageBoxButtons.OK);
            }
            if (FrmDieuChuyenKho.daluu == 1)
            {
                RefreshData();
                FrmDieuChuyenKho.daluu = 0;
            }
        }

        private void toolStripMenuItemQuyDoiMatHang_Click(object sender, EventArgs e)
        {
            int _MaVachID = StaticUtil.GetInt(this.grdViewMaVach.GetFocusedRowCellValue("MaVachID"), -1);

            if (_MaVachID != -1)
            {
                FrmChuyenKho.daluu = 0;
                FrmChuyenKho.MaVachBO = Model.MaVachBO.GetRootMaVachBO(_MaVachID);
                FrmChuyenKho frm = new FrmChuyenKho();
                frm.ShowDialog();
            }
            else
            {
                MessageBox.Show("Chưa chọn Hàng hóa và Mã vạch để chuyển đổi! ", "Thông báo", MessageBoxButtons.OK);
            }
            if (FrmChuyenKho.daluu == 1)
            {
                RefreshData();
                FrmChuyenKho.daluu = 0;
            }
        }

        private void toolStripMenuItemXuatBe_Click(object sender, EventArgs e)
        {
            int _MaVachID = StaticUtil.GetInt(this.grdViewMaVach.GetFocusedRowCellValue("MaVachID"), -1);

            if (_MaVachID != -1)
            {
                FrmXuatBe.daluu = 0;
                FrmXuatBe.MaVachBO = Model.MaVachBO.GetRootMaVachBO(_MaVachID);
                FrmXuatBe frm = new FrmXuatBe();
                frm.ShowDialog();
            }
            else
            {
                MessageBox.Show("Chưa chọn Hàng hóa và Mã vạch để xuất bể! ", "Thông báo", MessageBoxButtons.OK);
            }
            if (FrmXuatBe.daluu == 1)
            {
                RefreshData();
                FrmXuatBe.daluu = 0;
            }
        }
    }
}